package stackqueue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/7 18:42
 * 用两个队列实现栈
 */
public class MyStack {

    //倒序存储
    private Queue<Integer> inque;

    //辅助队列
    private Queue<Integer> inque1;

    public MyStack() {
        inque = new LinkedList<>();
        inque1 = new LinkedList<>();
    }

    public void push(int x) {
        inque1.offer(x);
        while (!inque.isEmpty()) {
            inque1.offer(inque.poll());
        }
        Queue<Integer> temp;
        temp = inque;
        inque = inque1;
        inque1 = temp;
    }

    /**
     * int pop() 移除并返回栈顶元素。
     * @return
     */
    public int pop() {
        return inque.poll();
    }

    public int top() {
        return inque.peek();
    }

    public boolean empty() {
        return inque.isEmpty();
    }

}
